package com.bw.day11.controller;

import java.io.File;
import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.bw.day11.entity.Claim;
import com.bw.day11.service.ClaimService;
import com.carrey.carrey_utils.FileUtils;
import com.carrey.carrey_utils.RandomUtils;
import com.github.pagehelper.PageInfo;

@Controller
public class ClaimController {
	@Autowired
	private ClaimService claimService;
	@RequestMapping("list")
	public String getlist(@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "2")Integer pageSize,Claim claim,Model model) {
		PageInfo<Claim> info = claimService.getlist(pageNum,pageSize,claim);
		model.addAttribute("info", info);
		model.addAttribute("claim", claim);
		return "list";
	}
	@RequestMapping("update")
	public String update(String id,Model model) {
		Claim claim = claimService.selectid(id);
		if (claim==null) {
			claim = new Claim();
		}
		model.addAttribute("claim", claim);
		
		return "update";
	}
	@RequestMapping("/toupdate")
	public String toupdate(@Validated @ModelAttribute Claim claim,MultipartFile file) throws IllegalStateException, IOException {
		if (!file.isEmpty()) {
			String originalFilename = file.getOriginalFilename();
			String fileExtensions = FileUtils.getFileExtensions(originalFilename);
			String uuid = RandomUtils.getUuid();
			File file2 = new File("E:/imgpic/"+uuid+fileExtensions);
			file.transferTo(file2);
			claim.setAppedix("/imgpic/"+uuid+fileExtensions);
		}
		claimService.insertclaim(claim);
		return "redirect:list";
	}
}
